<?PHP
////////////////////////////////////////////////////////////////////////////
//                                                                        //
// NOTICE OF COPYRIGHT                                                    //
//                                                                        //
// Dean`s Office for Moodle                                               //
// Электронный деканат                                                    //
// <http://deansoffice.ru/>                                               //
//                                                                        //
// Copyright (C) 2012-2999  Kirill Krasnoschekov (Кирилл Краснощёков)     //
//                                                                        //
// This program is free software: you can redistribute it and/or modify   //
// it under the terms of the GNU General Public License as published by   //
// the Free Software Foundation, either version 3 of the Licensen.        //
//                                                                        //
// This program is distributed in the hope that it will be useful,        //
// but WITHOUT ANY WARRANTY; without even the implied warranty of         //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          //
// GNU General Public License for more details.                           //
//                                                                        //
// You should have received a copy of the GNU General Public License      //
// along with this program.  If not, see <http://www.gnu.org/licenses/>.  //
//                                                                        //
////////////////////////////////////////////////////////////////////////////

//// Plugin's own library
require_once('lib.php');

//// Objects
$gb = new DOFGuestbook();
$gb->SetMessagesPerPage(8);



//// Timer
$timeStart = microtime();

//// Debug mode
define('DEBUG', false);        

//// Forms
$msg_add_form = new guestbook_message_add_form;
$msg_update_form = new guestbook_message_update_form;
$cmt_form = new guestbook_comment_form;

////Input
//Fetching data
if ( $msg_add_data = $msg_add_form->get_data() ) 
{
    //Message add
    $data = (array) $msg_add_data;
}
elseif ( $msg_update_data = $msg_update_form->get_data() ) 
{
    //Message updage
    $data = (array) $msg_update_data;
}
elseif ( $cmt_data = $cmt_form->get_data() ) 
{
    //Comment
    $data = (array) $cmt_data;
}
else
{
    //Something else through hyperlink (and hence GET)
    $data = $_GET;
}

//Handling data
list($posted_message, $posted_comment) = HandleData($data, $gb);

//Clearing forms
$msg_add_form->reset_submission();        
$msg_update_form->reset_submission();
$cmt_form->reset_submission();


//Total pages
$pages = $gb->CountPages();
//Selected page
if ( isset($data['page']) ) 
{
    $page = $data['page'];
    if ( $page > $pages - 1 ) 
    {
        $page = 0;
    }
}
else
{
    $page = 0;
}



////Output
//Templates
require_once(realpath(dirname(__FILE__)).'/lib/tmpl.dof.php');
//Global vars
$globalVars = array('title' => $DOF->get_string('title', 'gb'),
                    'page' => $page, 
                    'pages' => $pages);
//Presentation
$pr = new DOFPresentation
        ($tmpl, $globalVars, $msg_add_form, $msg_update_form, $cmt_form);

// Header
$DOF->modlib('nvg')->print_header(NVG_MODE_PORTAL);                
                
// Contents
$DOF->modlib('widgets')->print_box_start();
Display($pr, $gb, $posted_message, $posted_comment, $page, $timeStart);        
$DOF->modlib('widgets')->print_box_end();

// Footer
$DOF->modlib('nvg')->print_footer(NVG_MODE_PORTAL);
?>